package jmine.tec.test.http;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import jmine.tec.utils.collection.IteratorEnumeration;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

/**
 * @author takeshi
 */
public class MockHttpSession implements HttpSession, BeanFactoryAware {

    private Map<String, Object> attributes = new HashMap<String, Object>();

    private final String id = String.valueOf(System.identityHashCode(this));

    private int maxInactiveInterval;

    private ServletContext servletContext;

    /**
     * @param name String
     * @return Object
     */
    public Object getAttribute(final String name) {
        return this.attributes.get(name);
    }

    /**
     * @return null
     */
    public Enumeration<String> getAttributeNames() {
        return new IteratorEnumeration<String>(this.attributes.keySet());
    }

    /**
     * @return long
     */
    public long getCreationTime() {
        return 0;
    }

    /**
     * @return String
     */
    public String getId() {
        return this.id;
    }

    /**
     * @return long
     */
    public long getLastAccessedTime() {
        return 0;
    }

    /**
     * @return the maxInactiveInterval
     */
    public final int getMaxInactiveInterval() {
        return this.maxInactiveInterval;
    }

    /**
     * @return the servletContext
     */
    public final ServletContext getServletContext() {
        return this.servletContext;
    }

    /**
     * @return null
     */
    @SuppressWarnings("deprecation")
    public javax.servlet.http.HttpSessionContext getSessionContext() {
        return null;
    }

    /**
     * @param name String
     * @return Object
     */
    public Object getValue(final String name) {
        return this.getAttribute(name);
    }

    /**
     * @return {@link String}
     */
    public String[] getValueNames() {
        return this.attributes.keySet().toArray(new String[this.attributes.size()]);
    }

    /**
     * 
     */
    public void invalidate() {
        this.attributes.clear();
    }

    /**
     * @return boolean
     */
    public boolean isNew() {
        return false;
    }

    /**
     * @param name String
     * @param value Object
     */
    public void putValue(final String name, final Object value) {
        this.attributes.put(name, value);
    }

    /**
     * @param name String
     */
    public void removeAttribute(final String name) {
        this.attributes.remove(name);
    }

    /**
     * @param name String
     */
    public void removeValue(final String name) {
        this.attributes.remove(name);
    }

    /**
     * @param name string
     * @param value object
     */
    public void setAttribute(final String name, final Object value) {
        this.attributes.put(name, value);
    }

    /**
     * @param maxInactiveInterval the maxInactiveInterval to set
     */
    public final void setMaxInactiveInterval(final int maxInactiveInterval) {
        this.maxInactiveInterval = maxInactiveInterval;
    }

    /**
     * @param servletContext the servletContext to set
     */
    public final void setServletContext(final ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    /**
     * {@inheritDoc}
     */
    public void setBeanFactory(BeanFactory beanFactory) {
        // vazio
    }

}
